package echat.future.cn.echat.my.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityDividePlanBinding;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.DividePlanBean;
import echat.future.cn.echat.my.bean.PlanList;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.ImageLoadUtils;
import echat.future.cn.echat.my.utils.StatusBarUtils;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;

/**
 * 分成计划
 */
public class DividePlanActivity extends BaseActivity<ActivityDividePlanBinding>{

    private final static int TYPE_ZHUBO = 1;
    private final static int TYPE_PLANE = 2;
    private int type = TYPE_ZHUBO;

    @Override
    public int layoutId() {
        return R.layout.activity_divide_plan;
    }

    @Override
    public IBaseNavagation navagation() {
        return null;
    }

    @Override
    public void initView() {
        showPlane();
        bind.plan.setOnClickListener(v->showPlane());
        bind.zhubo.setOnClickListener(v->showZhubo());
        bind.btnExtension.setOnClickListener(v->startActivity(new Intent(this,InvitationsActivity.class)
                .putExtra(InvitationsActivity.EXTRA_TYPE,type)));
//        setData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.hideStatus(this);
        loadData();
        ImageLoadUtils.loadImage(this,UserHelper.getUser().getAvatar(),bind.circleImageView2);
        if(UserHelper.getWithdrawAccount().getMobile().equals("")){
            bind.llAcount.setVisibility(View.GONE);
            bind.noAccount.setVisibility(View.VISIBLE);
        }else{
            bind.llAcount.setVisibility(View.VISIBLE);
            bind.noAccount.setVisibility(View.GONE);

            bind.account.setText(UserHelper.getWithdrawAccount().getAlipay_account());
            bind.name.setText(UserHelper.getWithdrawAccount().getAlipay_username());
            bind.bindPohone.setText(UserHelper.getWithdrawAccount().getMobile());
        }
    }


    private void showPlane(){
        type = TYPE_PLANE;
        bind.llContribution.setVisibility(View.VISIBLE);
        bind.remindUser.setOnClickListener(v ->
                startActivity(new Intent(this,RemindUserListActivity.class)));
        bind.remindUsersTitle.setText("我推荐的用户");
        bind.llWithdraw.setOnClickListener(v -> startActivity(new Intent(this,UserWithdrawActivity.class)));
        bind.llContribution.setOnClickListener(v -> startActivity(new Intent(this,ContributionActivity.class)));
        bind.plan.setBackgroundResource(R.drawable.bg_6560e2_r_18);
        bind.zhubo.setBackgroundColor(Color.TRANSPARENT);
        setData();
    }

    private void showZhubo(){
        type = TYPE_ZHUBO;
        bind.llContribution.setVisibility(View.GONE);
        bind.remindUsersTitle.setText("我推荐的主播");
        bind.remindUser.setOnClickListener(v -> startActivity(new Intent(this,RemindZhuboActivity.class)));
        bind.llWithdraw.setOnClickListener(v -> startActivity(new Intent(this,ZhuboWithdrawActivity.class)));
        bind.zhubo.setBackgroundResource(R.drawable.bg_6560e2_r_18);
        bind.plan.setBackgroundColor(Color.TRANSPARENT);
        setData();
    }
    DividePlanBean dividePlanBean;
    PlanList mPlanList;

    /**
     * 分成计划比列
     */
    public void loadData(){
        new HttpUtilsRequst<>(this,
                HttpUtilsRequst.retrofitClient.create(UserApi.class)
        .getDividePlane())
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<List<DividePlanBean>>) dividePlanBeans -> {
                    try {
                        dividePlanBean = dividePlanBeans.get(0);

                        new HttpUtilsRequst<PlanList>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).planList(UserHelper.getToken(),"1"))
                                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<PlanList>) planList -> {
                                    mPlanList = planList;
                                    setData();
                                })
                                .start();
                    }catch (Exception e){
                        toast("获取分成计划失败");
                    }
                }).start();


    }

    private void setData(){
        if(dividePlanBean == null){
            return;
        }
        if(type == TYPE_ZHUBO) {
            //用户是主播的时候
            bind.ration.setText("分成比列"+dividePlanBean.getAnchor_proportions()+"%");
            if(mPlanList != null) {
                bind.remindUsers.setText(mPlanList.getList_anchor_num());
            }
        }else {
            //用户是计划的时候
            bind.ration.setText("分成比列"+dividePlanBean.getPlan_proportions()+"%");
            if(mPlanList != null) {
                bind.remindUsers.setText(mPlanList.getList_user_num());
                bind.contribution.setText(mPlanList.getContribution_total());
            }
        }
    }

}
